<template>
  <div class="home-hot">
    <HomePanel title="人气推荐" sub-title="人气爆款 不容错过">
      <!-- 面板内容 -->
      <!-- 设置一个父盒子 给个相对定位, 防止盒子跑偏 -->
      <div ref="target" style="position: relative; height: 406px">
        <!-- 添加动画效果 -->
        <transition name="fade">
          <ul ref="pannel" class="goods-list" v-if="list.length">
            <li v-for="item in list" :key="item.id">
              <RouterLink to="/">
                <img v-lazy="item.picture" alt="" />
                <p class="name">{{ item.title }}</p>
                <p class="desc">{{ item.alt }}</p>
              </RouterLink>
            </li>
          </ul>
          <!-- 骨架屏效果 -->
          <HomeSkeleton v-else bg="#f0f9f4"></HomeSkeleton>
        </transition>
      </div>
    </HomePanel>
  </div>
</template>

<script>
import HomeSkeleton from './home-skeleton.vue'
import HomePanel from './home-panel.vue'
import { findHot } from '@/api/home.js'
import { useLazyData } from '@/hooks/index.js'
export default {
  name: 'HomeHot',
  components: {
    HomePanel,
    HomeSkeleton
  },
  setup () {
    // 获取人气推荐数据
    // const list = ref([])
    // findHot().then((data) => {
    //   list.value = data.result
    // })
    // 组件懒加载
    const { target, result } = useLazyData(findHot)
    return { list: result, target }
  }
}
</script>

<style lang="less" scoped>
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 426px;
  li {
    width: 306px;
    height: 406px;
    .hoverShadow();
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding-top: 12px;
      text-align: center;
    }
    .desc {
      color: #999;
      font-size: 18px;
    }
  }
}
.fade {
  &-leave {
    &-active {
      position: absolute;
      width: 100%;
      transition: opacity 0.5s 0.2s;
      z-index: 1;
    }
    &-to {
      opacity: 0;
    }
  }
}
</style>
